<template>
  <div class="outmain ba_f">
    <div class="shuju_title mar_b20">
      <div class="shuju_title_text">
        <span>瓜分红包</span>
      </div>
    </div>
    <div class="">
      <el-form :inline="false" :model="form" class="demo-form-inline" label-width="130px">
<!--        <div>-->
<!--          <el-form-item label="手机号/姓名/昵称">-->
<!--            <el-input v-model="form.keyword" size="medium" placeholder="请输入联系人/电话" @input="selectType"></el-input>-->
<!--          </el-form-item>-->
<!--        </div>-->
<!--        <div class="flex ba_f">-->
<!--          <el-form-item label="查询日期：">-->
<!--            <div class="flex-bet">-->
<!--              <div>-->
<!--                <el-radio-group v-model="form.timeType" @change="selectType">-->
<!--                  <el-radio-button label="1">今天</el-radio-button>-->
<!--                  <el-radio-button label="2">近七天</el-radio-button>-->
<!--                  <el-radio-button label="3">近十五天</el-radio-button>-->
<!--                  <el-radio-button label="4">自定义时间</el-radio-button>-->
<!--                </el-radio-group>-->
<!--                <span class="mar_l20" v-if="form.timeType=='4'">-->
<!--                  <el-date-picker v-model="form.time" type="daterange" range-separator="至" start-placeholder="开始日期"-->
<!--                                  end-placeholder="结束日期" format="yyyy年MM月dd日" @change="selectType"></el-date-picker>-->
<!--                </span>-->
<!--              </div>-->
<!--            </div>-->
<!--          </el-form-item>-->
<!--        </div>-->
            <div>
              <el-button type="primary" @click="handleEdit()" size="medium">新增活动</el-button>
            </div>
      </el-form>
    </div>
    <div class="tabledata mar_t20">
      <el-table :data="list" v-loading="listLoading" :element-loading-text="elementLoadingText" stripe style="width: 100%">
        <el-table-column type="selection"></el-table-column>
        <el-table-column  label="活动名称" prop="name"></el-table-column>
        <el-table-column  label="红包个数"  prop="receiveNum"></el-table-column>
        <el-table-column label="有效期" width='400'>
          <template slot-scope="scope">
            <div class="flex">
              <div class="flex-col t_l">
                {{scope.row.startTime}}~{{scope.row.endTime}}
              </div>
            </div>
          </template>
        </el-table-column>
<!--        <el-table-column prop="state" label="活动状态" >-->
<!--          <template slot-scope="scope">-->
<!--            <div v-if="scope.row.state=='1'"><el-tag size="mini">未开始</el-tag></div>-->
<!--            <div v-if="scope.row.state=='2'"><el-tag size="mini" type="success">进行中</el-tag></div>-->
<!--            <div v-if="scope.row.state=='3'"><el-tag size="mini" type="danger">已结束</el-tag></div>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column
            prop="display"
            label="状态"
        >
          <template slot-scope="scope">
            <el-switch v-model="scope.row.display" active-value="1" inactive-value="2"
                       @change="changeSwitch(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <div class="flex">
              <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
              <el-button size="mini" type="text" @click="seeDetail(scope.row)" class="seebtn">查看</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total="page.total" :page-size="page.pagesize" :current-page.sync="page.currentPage" @current-change="handleCurrentChange"></el-pagination>
<!--      <edit ref="edit" @fetchData="fetchData"></edit>-->
    </div>
  </div>
</template>
<script>
import {dividendList, modifyDividend} from '@/api/plug';
// import Edit from "./components/edit";
import {mapState} from 'vuex'
import {changeDate} from "@/utils";
export default {
  components: {},
  created() {
    this.fetchData();
  },
  data() {
    return {
      form: {
        keyword: '',
        timeType: '1',
        time: [new Date(), new Date()],
      },
      list: [],
      listLoading: false,
      selectRows: '',
      elementLoadingText: '正在加载...',
      page: {
        total: 0,
        size: 10,
        currentPage: 1
      },
    };
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      var params = {
        page: this.page.currentPage,
      };
      const { data, count } = await dividendList(params);
      this.list = data;
      this.page.total = count;
      this.listLoading = false;
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.fetchData();
    },
    selectType() {
      this.fetchData();
    },
    handleEdit2(row) {
      if (row.id) {
        this.$refs["edit"].showEdit(row,1);
      } else {
        this.$refs["edit"].showEdit();
      }
    },
    handleEdit(row) {
      if (row) {
        this.$router.push(`/dividendadd?id=${row.id}`);
      } else {
        this.$router.push({ path: 'dividendadd' });
      }
    },
    async changeSwitch(row) {
      const {msg} = await modifyDividend({id: row.id, display: row.display,type:1});
      this.$baseMessage(msg, "success");
      this.fetchData();
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm("你确定要删除当前项吗", null, async () => {
          const {msg} = await modifyDividend({id: row.id,type:2});
          this.$baseMessage(msg, "success");
          this.fetchData();
        });
      } else {
        if (this.selectRows.length > 0) {
          const id = this.selectRows.map((item) => item.id);
          this.$baseConfirm("你确定要删除选中项吗", null, async () => {
            const {msg} = await modifyDividend({id,type:2});
            this.$baseMessage(msg, "success");
            this.fetchData();
          });
        } else {
          this.$baseMessage("未选中任何行", "error");
          return false;
        }
      }
    },
    seeDetail(row) {
      this.$router.push(`/divideinfotwo?id=${row.id}`);
    },
  }
};
</script>
